package com.retch.wenchengmao.sqlitedeme2;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity implements View.OnClickListener {
    Button addOneData;
    Student student2=null;
    SQLiteDatabase sqLiteDatabase;
    MySQLiteHelper<Student> mySQLiteHelper;
    List<Student> studentInfo;
    InfoAdapter infoAdapter;
    private EditText search_content;
    private Button search;
    private ListView listView;
    String searchContent;
    int id;
    String name;
    double tuition;
    float price;
    Student student=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

//        Student student2=new Student(33,"的随时随地2",100);
//        mySQLiteHelper.upDate(Student.class,student2,33);

        List<Object> listInfo=new ArrayList<>();
        Student s3=new Student(1,"阿大",100,200);
        Student s4=new Student(2,"阿二",300,400);
        Student s5=new Student(3,"阿三",500,600);
        Student s6=new Student(4,"阿四",700,800);
        listInfo.add(s3);
        listInfo.add(s4);
        listInfo.add(s5);
        listInfo.add(s6);
        student=new Student(1,"阿大",100.0,200);
        mySQLiteHelper = new MySQLiteHelper(MainActivity.this, "db_test5", null, 1);
        mySQLiteHelper.save(student);
        mySQLiteHelper.insertMultipleData(listInfo);
//        mySQLiteHelper.insertMultipleData(listInfo);
        List<Student> studentList = mySQLiteHelper.queryAllData(student.getClass());
        Log.d("studentList1", String.valueOf(studentList.size()));
        infoAdapter = new InfoAdapter(MainActivity.this, studentList);
        listView.setAdapter(infoAdapter);
        infoAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onResume()
    {
        super.onResume();
        List<Student> studentList = mySQLiteHelper.queryAllData(student.getClass());
        Log.d("studentList1", String.valueOf(studentList.size()));
        infoAdapter = new InfoAdapter(MainActivity.this, studentList);
        listView.setAdapter(infoAdapter);
        infoAdapter.notifyDataSetChanged();
    }


    public void initView() {
        addOneData=(Button) findViewById(R.id.add_filed);
        addOneData.setOnClickListener(this);
        search_content = (EditText) findViewById(R.id.search_content);
        search = (Button) findViewById(R.id.search_btn);
        search.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.list_view);
        searchContent = search_content.getText().toString();
        studentInfo = new ArrayList<>();
        clickItem();
    }

    public void clickItem() {
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                List<Student> listStudent=mySQLiteHelper.queryAllData(student.getClass());
                student2=listStudent.get(position);
                final View contentView = MainActivity.this.getLayoutInflater().inflate(R.layout.item_popupwindow, null);
                // 创建PopupWindow对象，指定宽度和高度
                final PopupWindow window = new PopupWindow(contentView, 400, 600);
                // 2016/5/17 设置动画
                window.setAnimationStyle(R.style.popup_window_anim);
                //  2016/5/17 设置背景颜色
                window.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F8F8F8")));
                // 2016/5/17 设置可以获取焦点
                window.setFocusable(true);
                // 2016/5/17 设置可以触摸弹出框以外的区域
                window.setOutsideTouchable(true);
                // 更新popupwindow的状态
                window.update();
                // 2016/5/17 以下拉的方式显示，并且可以设置显示的位置
                contentView.findViewById(R.id.modify_data).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        Intent intent=new Intent();
                        intent.setClass(MainActivity.this,ModifyActivity.class);
                        intent.putExtra("id",student2.getId());
                        intent.putExtra("name",student2.getName());
                        intent.putExtra("tuition",student2.getTuition());
                        intent.putExtra("price",student2.getPrice());
                        startActivity(intent);
                    }
                });
                contentView.findViewById(R.id.delete_data).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        window.dismiss();
                        Intent intent=new Intent();
                        intent.setClass(MainActivity.this,DeleteOneDataActivity.class);
                        intent.putExtra("id",student2.getId());
                        intent.putExtra("name",student2.getName());
                        intent.putExtra("tuition",student2.getTuition());
                        intent.putExtra("price",student2.getPrice());
                        startActivity(intent);
                    }
                });
                window.showAsDropDown(view, 0, 20);
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_filed:
                startActivity(new Intent(MainActivity.this,AddContentActivity.class));
                break;
            case R.id.search_btn:
                search();
                break;
            default:
                break;
        }
    }

    public void search()
    {
        if(search.getText().toString().equals(""))
        {
            Toast.makeText(MainActivity.this,"id不能为空!",Toast.LENGTH_LONG).show();
        }
        else
        {
            List<Student> studentList = mySQLiteHelper.querySingleData(Student.class,search_content.getText().toString());
            Log.d("studentList2", String.valueOf(studentList.size()));
            infoAdapter = new InfoAdapter(MainActivity.this, studentList);
            listView.setAdapter(infoAdapter);
            infoAdapter.notifyDataSetChanged();
        }

    }

    class InfoAdapter extends BaseAdapter {
        public LayoutInflater layoutInflater;
        public Context context;
        List<Student> list;

        public InfoAdapter(Context context, List<Student> list) {
            this.context = context;
            this.list = list;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        public Student getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.item_student_info, null);
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            Student item = getItem(position);
            holder.name.setText(item.getName()+"");
            holder.info_id.setText(item.getId()+"");
            holder.tuition.setText(item.getTuition()+"");
            holder.price.setText(item.getPrice()+"");
            return convertView;
        }
    }

    class ViewHolder {
        TextView info_id;
        TextView name;
        TextView tuition;
        TextView price;
        public ViewHolder(View convertView) {
            info_id = (TextView) convertView.findViewById(R.id.info_id);
            name = (TextView) convertView.findViewById(R.id.name);
            tuition=(TextView)convertView.findViewById(R.id.tuition);
            price=(TextView)convertView.findViewById(R.id.price);
        }
    }
}
